package com.whut.whw.find;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.whut.whw.R;
import com.whut.whw.personalinfo.mywant.MyWantEntity;
import com.whut.whw.util.ConvertUtils;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <pre>
 *     author : 杨丽金
 *     time   : 2017/06/22
 *     desc   : 物品列表Recycleview适配器
 *     version: 1.0
 * </pre>
 */
public class MyGoodsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * recyclerView的点击、长按事件
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }


    // 数据源
    private List<GoodsEntity> datas = null;
    // context
    private Context context;
    private static final int TYPE_ITEM = 0;// item布局
    private static final int TYPE_FOOTER = 1;// 尾布局
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public MyGoodsListAdapter(Context context, List<GoodsEntity> datas) {
        this.context = context;
        this.datas = datas;
    }

    public List<GoodsEntity> getDatas() {
        return datas;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas.size() == 0 ? 0 : datas.size() + 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_goods, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(context).inflate(R.layout.default_loading, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            GoodsEntity goodsItem = datas.get(position);
            final String internetUrl = "http://ww4.sinaimg.cn/large/610dc034jw1f41lxgc3x3j20jh0tcn14.jpg";
            Glide.with(context).load(internetUrl).asBitmap().placeholder(R.mipmap.ic_launcher).error(R.drawable.girl).centerCrop().into(new SimpleTarget<Bitmap>(ConvertUtils.dp2px(42), ConvertUtils.dp2px(42)) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    itemViewHolder.mHeadImg.setImageBitmap(resource);
                }
            });
            Glide.with(context).load(internetUrl).asBitmap().placeholder(R.mipmap.ic_launcher).error(R.drawable.girl).centerCrop().into(new SimpleTarget<Bitmap>(ConvertUtils.dp2px(300), ConvertUtils.dp2px(200)) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    itemViewHolder.mMainImg.setImageBitmap(resource);
                }
            });
           /* String tag = (String) viewHolder.mHeadImg.getTag();
            if (!StringUtils.equals(internetUrl, tag)) {
                viewHolder.mHeadImg.setImageResource(R.mipmap.ic_launcher);
            }
            Glide.with(context).load(internetUrl).asBitmap().placeholder(R.mipmap.ic_launcher)
                    .error(R.drawable.girl).centerCrop().into(new SimpleTarget<Bitmap>
                    (ConvertUtils.dp2px(36), ConvertUtils.dp2px(36)) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap>
                        glideAnimation) {
                    viewHolder.mHeadImg.setTag(internetUrl);
                    viewHolder.mHeadImg.setImageBitmap(resource);
                }
            });*/
            itemViewHolder.mTvName.setText(goodsItem.getName());
//                itemViewHolder.mTvAddress.setText(goodsItem.getAddress());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            StringBuilder publishTimeAndAddressStr = new StringBuilder();
            publishTimeAndAddressStr.append(sdf.format(goodsItem.getPublishTime()))
                    .append(" 来自：")
                    .append(goodsItem.getAddress());
            itemViewHolder.mTvPublishTimeAndAddress.setText(publishTimeAndAddressStr.toString());
            itemViewHolder.mTvPrice.setText(goodsItem.getPrice());
            itemViewHolder.mTvTitle.setText(goodsItem.getTitle());
            itemViewHolder.mTvDescription.setText(goodsItem.getDescription());
            itemViewHolder.mTvCounts.setText("评论" + goodsItem.getCommentCount() +
                    "；点赞" + goodsItem.getLikeCount() + "；想要" + goodsItem.getLikeCount());
              /*  itemViewHolder.mTvCommentCount.setText(goodsItem.getCommentCount() + "");
                itemViewHolder.mTvLikeCount.setText(goodsItem.getLikeCount() + "");
                itemViewHolder.mTvWantCount.setText(goodsItem.getWantCount() + "");*/
            // 如果设置了回调，则设置点击事件
            if (mOnItemClickListener != null) {
                itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = itemViewHolder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(itemViewHolder.itemView, pos);
                    }
                });
                itemViewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        int pos = itemViewHolder.getLayoutPosition();
                        mOnItemClickListener.onItemLongClick(itemViewHolder.itemView, pos);
                        return true;
                    }
                });
            }
        }
    }


    //自定义的ViewHolder，持有每个Item的的所有界面元素
    class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView mHeadImg;
        ImageView mMainImg;

        TextView mTvName;
        //            TextView mTvAddress;
        TextView mTvPublishTimeAndAddress;
        TextView mTvPrice;
        TextView mTvTitle;
        TextView mTvDescription;
        /*TextView mTvCommentCount;
        TextView mTvLikeCount;
        TextView mTvWantCount;*/
        TextView mTvCounts;

        public ItemViewHolder(View view) {
            super(view);
            mHeadImg = (ImageView) view.findViewById(R.id.civ_goodsItem_headImg);
            mMainImg = (ImageView) view.findViewById(R.id.iv_goodsItem_mainImg);

            mTvName = (TextView) view.findViewById(R.id.tv_goodsItem_name);
//                mTvAddress = (TextView) view.findViewById(R.id.tv_goodsItem_address);
            mTvPublishTimeAndAddress = (TextView) view.findViewById(R.id.tv_goodsItem_pulishTimeAndAddress);
            mTvPrice = (TextView) view.findViewById(R.id.tv_goodsItem_price);
            mTvTitle = (TextView) view.findViewById(R.id.tv_goodsItem_title);
            mTvDescription = (TextView) view.findViewById(R.id.tv_goodsItem_description);
               /* mTvCommentCount = (TextView) view.findViewById(R.id.tv_goodsItem_commentCount);
                mTvLikeCount = (TextView) view.findViewById(R.id.tv_goodsItem_likeCount);
                mTvWantCount = (TextView) view.findViewById(R.id.tv_goodsItem_wantCount);*/
            mTvCounts = (TextView) view.findViewById(R.id.tv_goodsItem_counts);
        }
    }

    // 尾布局ViewHolder
    class FootViewHolder extends RecyclerView.ViewHolder {

        public FootViewHolder(View view) {
            super(view);
        }
    }

    /**
     * 更新数据源
     * @param datas
     */
    public void updateData(List<GoodsEntity> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    /**
     * 向数据源中追加数据
     * @param datas
     */
    public void appendData(List<GoodsEntity> datas) {
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    /**
     * 删除某一条目
     * @param position
     */
    public void removeData(int position) {
        datas.remove(position);
        notifyItemRemoved(position);
    }


}


